var express = require('express');
var router = express.Router();
var _ = require('underscore');
var site_info =	require("../config").site_info;
var db_tag = require("../dao/tag-dao");

/* GET home page. */
router.get('/', function(req, res, next) {
	var cur_site_info = _.extend({}, site_info, {
        page:{
            title: "tag - " + site_info.page.title,
            name: "tag",
            intro: "",
            intro_desc: "",
        }
    });

	res.render('tag', {
		site_info: cur_site_info
	});
});


router.get('/get-tag-list', function(req, res, next) {
    var condition = {
        searchType: req.param("searchType") || 1,
        searchCriteria: {
            id: req.param("id") || 0
        }
    }
    db_tag.search(condition, function(err, result){
        if(err){
            next(err);
        }
        return res.json({
            status: site_info.return_type.success, 
            result: result,
            total: 1
        });
    });
});

router.post('/operate-tag-submit', function(req, res, next) {
    var tag = {
        id: req.param("id") || 0,
        title: req.param("title") || "",
        color: req.param("color") || ""
    };
    console.log(tag)

    db_tag.save(tag, function(err, result){
        if(err){
            next(err);  
        }
        //console.log(result)
        return res.json({
            status: site_info.return_type.success,
            result:{
                id: result
            }
        });
    });
});

module.exports = router;
